package fr.syntheses.blog.Service.mailler;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailServices {

	
	String error = "";

	public String sendMail(String formMailContenu, String formMailAdress, String formMailSujet) {

		System.out.println("==> Entering in mailService : " + formMailAdress);


		String strHost = "smtp.orange.fr";
		String strFrom = "adminBlog@blog.com";
		String strTo = formMailAdress;
		String strMailer = "MonMailer";

		try {
			Properties pProps = System.getProperties ();

			pProps.put ("mail.smtp.host", strHost);

			Session sSession = Session.getDefaultInstance (pProps,null);

			Message msgMessage = new MimeMessage (sSession);

			msgMessage.setFrom (new InternetAddress (strFrom));

			msgMessage.setRecipients (Message.RecipientType.TO, InternetAddress.parse (strTo, false));

			msgMessage.setSubject (formMailSujet);

			msgMessage.setText (formMailContenu);

			msgMessage.setHeader ("X-Mailer",strMailer);

			msgMessage.setSentDate (new Date ());

			Transport.send (msgMessage);

			System.out.println ("Message envoy�");

		} catch (Exception e) {

			System.out.println ("Erreur : " +e.getMessage ());

			error = e.getMessage ();


		}
		
		return error;

	}
}